home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / pascal / tptc17tc.zip / VARREC.PAS < prev   
Pascal/Delphi Source File  |  1988-03-25  |  2KB  |  67 lines

  1.  
  2. (*
  3.  * Examples of variant record types
  4.  *)
  5.  
  6. program Variant_Record_Example;
  7.  
  8. type 
  9.      Kind_Of_Vehicle = (Car,Truck,Bicycle,Boat);
  10.  
  11.      Vehicle = record
  12.        Owner_Name   : string[25];
  13.        Gross_Weight : integer;
  14.        Value        : real;
  15.        case What_Kind : Kind_Of_Vehicle of
  16.          Car     : (Wheels : integer;
  17.                     Engine : string[8]);
  18.          Truck   : (Motor  : string[8];
  19.                     Tires  : integer;
  20.                     Payload : integer);
  21.          Bicycle : (Tyres   : integer);
  22.          Boat    : (Prop_Blades : byte;
  23.                     Sail    : boolean;
  24.                     Power   : string[8]);
  25.        end; (* of record *)
  26.  
  27. var 
  28.     Sunfish,Ford,Schwinn,Mac : Vehicle;
  29.  
  30. begin  (* main program *)
  31.    Ford.Owner_Name := 'Walter'; (* fields defined in order *)
  32.    Ford.Gross_Weight := 5750;
  33.    Ford.Value := 2595.00;
  34.    Ford.What_Kind := Truck;
  35.    Ford.Motor := 'V8';
  36.    Ford.Tires := 18;
  37.    Ford.Payload := 12000;
  38.  
  39.    with Sunfish do begin
  40.       What_Kind := Boat; (* fields defined in random order *)
  41.       Sail := TRUE;
  42.       Prop_Blades := 3;
  43.       Power := 'wind';
  44.       Gross_Weight := 375;
  45.       Value := 1300.00;
  46.       Owner_Name := 'Herman and George';
  47.    end;
  48.  
  49.    Ford.Engine := 'flathead';  (* tag-field not defined yet but it *)
  50.    Ford.What_Kind := Car;      (* must be before it can be used    *)
  51.    Ford.Wheels := 4;
  52.       (* notice that the non variant part is not redefined here *)
  53.  
  54.    Mac := Sunfish; (* entire record copied, including the tag-field *)
  55.  
  56.    if Ford.What_Kind = Car then        (* this should print *)
  57.       Writeln(Ford.Owner_Name,' owns the car with a ',Ford.Engine,
  58.               ' engine');
  59.  
  60.    if Sunfish.What_Kind = Bicycle then  (* this should not print *)
  61.       Writeln('The sunfish is a bicycle which it shouldn''t be');
  62.  
  63.    if Mac.What_Kind = Boat then         (* this should print *)
  64.       Writeln('The mac is now a boat with',Mac.Prop_Blades:2,
  65.                ' propeller blades.');
  66. end.  (* of main program *)
  67.